import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import cn.edu.zut.cs.Business.readerService;
import cn.edu.zut.cs.Model.Reader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.io.PrintWriter;

@WebServlet(name="registerServlet",value="/registerServlet")
public class registerServlet extends HttpServlet {
    readerService readerService;
    Reader reader;
    @Override
    public void init(ServletConfig config) throws ServletException{
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,config.getServletContext());
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
        PrintWriter out=response.getWriter();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String ID=request.getParameter("ID");
        String password=request.getParameter("rpassword");
        String name=request.getParameter("name");
        String num=request.getParameter("num");
        if(ID.equals("")||ID.equals(null)||password.equals("")||password.equals(null)){
            JOptionPane.showMessageDialog(null, "用户号或密码不能为空","标题【警告】" , JOptionPane.ERROR_MESSAGE);
            response.sendRedirect("register.html");
            return;
        }else{
            reader.setID(ID);
            reader.setRpassword(password);
            reader.setName(name);
            reader.setNum(num);
            readerService.insert_reader(reader);
            PrintWriter out=response.getWriter();
            out.println(ID);
            out.println(password);
            out.println(name);
            out.println(num);
            response.sendRedirect("adminlogin.html");
        }
    }
    @Autowired
    public readerService getReaderService() {
        return readerService;
    }
    @Autowired
    public void setReaderService(readerService readerService) {
        this.readerService = readerService;
    }

    @Autowired
    public Reader getReader() {
        return reader;
    }
    @Autowired
    public void setReader(Reader reader) {
        this.reader = reader;
    }
}